package git.determinal.template.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.cj.util.StringUtils;
import git.determinal.template.domin.dto.SearchDto;
import git.determinal.template.domin.view.TemplateView;
import git.determinal.template.service.TemplateService;
import git.determinal.template.view.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import git.determinal.template.domin.dto.TemplateDto;

@Api(tags = "模板")
@RequestMapping("/template")
@RestController
public class TemplateController {
    @Autowired
    private TemplateService serviceTemplate;
    /**
     * @Description 更新模板
     * @Param TemplateDto 模板dto
     * @return 更新结果
     **/
     @ApiOperation(value = "更新模板", notes = "传入id为更新，不传为新增")
     @PostMapping("/update")
     public Result<String> update(@RequestBody TemplateDto templateDto) {
        if (templateDto == null) {
            return Result.error("templateDto为空", null);
        }
        serviceTemplate.updateEntity(templateDto);
        return Result.ok("更新成功");
    }
    
    /**
     * @Description 删除模板
     * @Param ids 要删除的模板id集合
     * @return 删除结果
     **/
     @ApiOperation(value = "删除模板", notes = "多个id用逗号拼接")
     @PostMapping("/delete")
     public Result<String> delete(@ApiParam(value = "模板的id，多个用逗号拼接") @RequestParam (defaultValue = "") String ids) {
        if (StringUtils.isNullOrEmpty(ids)) {
            return Result.error("ids为空", null);
        }
        serviceTemplate.deleteEntity(ids);
        return Result.ok("删除成功");
    }

    /**
     * @Description 分页查询
     * @Param searchContent 查询内容
     * @return 查询结果
     **/
     @ApiOperation(value = "分页查询", notes = "通过开始时间，结束时间，页码，页面大小，查询结果")
     @PostMapping("/page")
     public Result<Page<TemplateView>> page(@RequestBody SearchDto searchDto) {
        Page<TemplateView> page = serviceTemplate.pageContent(searchDto);
        return Result.ok(page);
    }

    /**
     * @Description 通过单个id获取模板
     * @Param id 单个模板的id
     * @return 模板实体
     **/
     @ApiOperation(value = "获取模板", notes = "通过单个id获取模板")
     @PostMapping("/getById")
     public Result<TemplateView> getById(@ApiParam(value = "单个模板的id") @RequestParam (defaultValue = "") String id) {
        if (StringUtils.isNullOrEmpty(id)) {
            return Result.error("id为空", null);
        }
        TemplateView templateView = serviceTemplate.getEntity(id);
        return Result.ok(templateView);
    }
}